import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AiDutyStatusRecordsApi } from '#/api/moxi/aiDutyStatusRecords';
import dayjs from 'dayjs';
import { handleTree } from '@vben/utils';

import { z } from '#/adapter/form';
import { getDeptList } from '#/api/system/dept';
import { getSimplePostList } from '#/api/system/post';
import {
    CommonStatusEnum,
    DICT_TYPE,
    getDictOptions,
} from '#/utils';
import Dayjs from 'dayjs';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
    return [
        {
            component: 'Input',
            fieldName: 'id',
            dependencies: {
                triggerFields: [''],
                show: () => false,
            },
        },
        {
            fieldName: 'username',
            label: '用户名称',
            component: 'Input',
            rules: 'required',
        },
        {
            label: '用户密码',
            fieldName: 'password',
            component: 'InputPassword',
            rules: 'required',
            dependencies: {
                triggerFields: ['id'],
                show: (values) => !values.id,
            },
        },
        {
            fieldName: 'nickname',
            label: '用户昵称',
            component: 'Input',
            rules: 'required',
        },
        {
            fieldName: 'deptId',
            label: '归属部门',
            component: 'ApiTreeSelect',
            componentProps: {
                api: async () => {
                    const data = await getDeptList();
                    return handleTree(data);
                },
                labelField: 'name',
                valueField: 'id',
                childrenField: 'children',
                placeholder: '请选择归属部门',
                treeDefaultExpandAll: true,
            },
        },
        {
            fieldName: 'postIds',
            label: '岗位',
            component: 'ApiSelect',
            componentProps: {
                api: getSimplePostList,
                labelField: 'name',
                valueField: 'id',
                mode: 'multiple',
                placeholder: '请选择岗位',
            },
        },
        {
            fieldName: 'email',
            label: '邮箱',
            component: 'Input',
            rules: z.string().email('邮箱格式不正确').or(z.literal('')).optional(),
        },
        {
            fieldName: 'mobile',
            label: '手机号码',
            component: 'Input',
        },
        {
            fieldName: 'sex',
            label: '用户性别',
            component: 'RadioGroup',
            componentProps: {
                options: getDictOptions(DICT_TYPE.SYSTEM_USER_SEX, 'number'),
                buttonStyle: 'solid',
                optionType: 'button',
            },
            rules: z.number().default(1),
        },
        {
            fieldName: 'status',
            label: '用户状态',
            component: 'RadioGroup',
            componentProps: {
                options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
                buttonStyle: 'solid',
                optionType: 'button',
            },
            rules: z.number().default(CommonStatusEnum.ENABLE),
        },
        {
            fieldName: 'remark',
            label: '备注',
            component: 'Textarea',
        },
    ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
    return [
        {
            fieldName: 'name',
            label: '员工姓名',
            component: 'Input',
            componentProps: {
                placeholder: '请输入员工姓名',
                allowClear: true,
            },
        },
        {
            fieldName: 'ofDate',
            label: '查询时间',
            component: 'DatePicker',
            // defaultValue: dayjs().format('YYYY-MM-DD'), // 初始值为字符串格式
            componentProps: {
                picker: 'date',
                allowClear: true,
                valueFormat: 'YYYY-MM-DD',
                format: 'YYYY-MM-DD',
            },
        },
    ];
}

/** 列表的字段 */
export function useGridColumns<T = AiDutyStatusRecordsApi.AiDutyStatusRecords>(
    onStatusChange?: (
        newStatus: number,
        row: T,
    ) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
    return [
        {
            field: 'id',
            title: '序号',
        },
        {
            field: 'name',
            title: '员工姓名',
        },
        {
            field: 'deptNames',
            title: '所属部门 ',
        },
        {
            field: 'onDutyParts',
            title: '在岗开始时间段',
        },
        {
            field: 'onDutyDuration',
            title: '在岗时长',
            formatter: ({ cellValue }) => {
                if (!cellValue || cellValue === 0) {
                    return '0小时';
                }
                
                const hours = Math.floor(cellValue / 60);
                const minutes = cellValue % 60;
                
                if (minutes === 0) {
                    return `${hours}小时`;
                } else {
                    return `${hours}小时${minutes}分钟`;
                }
            }
        },
        {
            field: 'offDutyParts',
            title: '离岗结束时间',
        },
        {
            field: 'offDutyDuration',
            title: '离岗结时长',
            formatter: ({ cellValue }) => {
                if (!cellValue || cellValue === 0) {
                    return '0小时';
                }
                
                const hours = Math.floor(cellValue / 60);
                const minutes = cellValue % 60;
                
                if (minutes === 0) {
                    return `${hours}小时`;
                } else {
                    return `${hours}小时${minutes}分钟`;
                }
            }
        },
        {
            field: 'onDutyRate',
            title: '在岗率'
        },
        {
            title: '操作',
            width: 180,
            fixed: 'right',
            slots: { default: 'actions' },
        },
    ];
}
